////////////////////////////////////////////page_parkIndexFlight////////////////////////////////
$(document).on("pageInit", "#page_parkIndexFlight", function(e, pageId, $page) {
	              //清空parkIndexFligth查询信息////////////////////////////////////
                setTimeout(function(){
                	$.showPreloader()
                },1)
	            $('.flightInfo-box-cx-list').empty()
	            $('.flightNum').val('')
	            $('.flightDate').val('')
	            $('.flightInfo-box-zdy-list-box').css('display','none')

	             //清空parkIndexFligth查询信息///////////////////////////////
	              key=my.get_key(my.getCookie('token'))
	              var minutesss = function (n) {
                        if(n>=0 && n<=15){
                            return '00';
                        }
                        if(n>15 && n<=30){
                            return '30';
                        }
                        if(n>31 && n<=45){
                            return '45';
                        }
                        if(n>45 && n<=60){
                            return '45';
                        }
                    };
	                var formatNumber = function (n) {
                        return n < 10 ? "0" + n : n;
                    };
	              //航班查询方式切换
                  $('#flightInfo-box-cx').click(function(){
                  	  $(this).addClass('active').siblings().removeClass('active')
                  	  $('.flightInfo-box-zdy').hide()
                  	  $('.flightInfo-box-cx').show()
                  	  $('.info-head').show()
                  	  if($('.flight-select').css('display')=='none'){
                  	  	 $('.dt-content').css('top','9rem')
                  	  }else{
                  	  	 $('.dt-content').css('top','11.4rem')
                  	  }

                  })
                  $('#flightInfo-box-zdy').click(function(){
                  	  $(this).addClass('active').siblings().removeClass('active')
                  	  $('.flightInfo-box-cx').hide()
                  	  $('.flightInfo-box-zdy').show()
                  	  $('.info-head').hide()
                  	  if($('.flight-select').css('display')=='none'){
                  	  	 $('.dt-content').css('top','4.4rem')
                  	  }else{
                  	  	 $('.dt-content').css('top','6.4rem')
                  	  }

                  })
                  //航班查询>选择时间
                  var now_time_flight=new Date()
                  var now_time_flight_s=now_time_flight.getFullYear()+'-'+(now_time_flight.getMonth()+1)+'-'+now_time_flight.getDate()
                  var now_time_cal=now_time_flight_s
                  var tmp1=now_time_flight_s.split("-")
                  now_time_flight_s=tmp1[0]+'-'+(tmp1[1].length<2?'0'+tmp1[1]:tmp1[1])+'-'+(tmp1[2].length<2?'0'+tmp1[2]:tmp1[1]);

                  $(".flightInfo-box-cx-time").val(now_time_cal)
                  $(".flightInfo-box-cx-time").calendar({

                  })
                  //航班查询>出发&&到达城市选择
                   my.citySupport(function(data){  //返回城市
                   	console.log(data)
                       var city=[],cityvalue=[];
                       for(var i=0;i<data.length;i++){
                     	    city[i]=data[i].airport_name;
                     	    cityvalue[i]=data[i].airport_code;
                       }
                      $('.flightInfo-box-cx-from').picker({   //出发
                  	         toolbarTemplate: '<header class="bar bar-nav">\<button class="button button-link pull-right close-picker">确定</button>\<h1 class="title">出发城市</h1>\</header>',

                  	         cols: [
                                     {
                                         textAlign: 'center',
                                         width:'100%',
                                         values: cityvalue,
                                         displayValues:city
                                      }
                             ],
//                           onClose:function(value){
//                	              $('.flightInfo-box-cx-from').val(value.value[0])
//                           },
                             formatValue: function (p, values, displayValues) {//格式化
                             	$('.flightInfo-box-cx-from').attr('airport_code',values);
                                	return displayValues;
                                }
                      });
                      $('.flightInfo-box-cx-to').picker({  //到达
                  	           toolbarTemplate: '<header class="bar bar-nav">\<button class="button button-link pull-right close-picker">确定</button>\<h1 class="title">到达城市</h1>\</header>',
                  	           cols: [
                                        {
                                             textAlign: 'center',
                                             width:'100%',
                                             values: cityvalue,
                                         	displayValues:city
                                         }
                                ],
//                              onClose:function(value){
//                                       	$('.flightInfo-box-cx-to').val(value.value[0])
//                              },
                                formatValue: function (p, values, displayValues) {//格式化
                                	$('.flightInfo-box-cx-to').attr('airport_code',values);
                                	return displayValues;
                                }
                     })
                      $(".flightInfo-box-cx-from,.flightInfo-box-cx-to").picker("open");
                      $(".flightInfo-box-cx-from,.flightInfo-box-cx-to").picker("close");
                      $(".flightInfo-box-cx-from").picker("setValue", [cityvalue[0]]);
                      $(".flightInfo-box-cx-to").picker("setValue", [cityvalue[3]]);
                     $.hidePreloader()
                   })
                 //航班查询>城市互换
                 $('.info-head-a-qh').off('click').on('click',function(){
                 	var from=$('.flightInfo-box-cx-from').val()
                 	var from_code=$('.flightInfo-box-cx-from').attr('airport_code')
                 	var to=$('.flightInfo-box-cx-to').val()
                 	var to_code=$('.flightInfo-box-cx-to').attr('airport_code')
                 	console.log(from+''+from_code)
                 	console.log(to+''+to_code)
                 	$('.flightInfo-box-cx-from').val(to).attr('airport_code',to_code)
                 	$('.flightInfo-box-cx-to').val(from).attr('airport_code',from_code)
                 })
                 //航班查询>执行查询
                 $('.flightInfo-box-cx-search').off('click').on('click',function(){
                 	  $('.flightInfo-box-cx-list').empty()
                 	  $.showPreloader()
                 	  var info={
                 	  	start_city:$('.flightInfo-box-cx-from').attr('airport_code'),
                 	  	end_city:$('.flightInfo-box-cx-to').attr('airport_code'),
                 	  	by_sort:0,
                 	  	page_now:1,
                 	  	plan_date:$(".flightInfo-box-cx-time").val(),
                 	  	page_size:'1000'
                 	  }
                 	  console.log(info)
                 	  info=my.cryptoASEencrypt(JSON.stringify(info),key);
                 	  $.ajax({
                 	  	type:"post",
                 	  	url:api.flightcitypan,
                 	  	async:true,
                 	  	data:{
                               'api_value':info
                        },
                 	  	error:function(){
                 	  		console.log('ajax报错')
                 	  	},
                 	  	complete:function(){
                 	  		setTimeout(function(){
                 	  			$.hidePreloader()
                 	  		},1000)
                 	  	},
                 	  	success:function(data){
                 	  	  if(data.match("err_code")!=null){
                               switch(JSON.parse(data).err_code)
                               {
                               	    case 40003:
                                                            my.tokenExpire()
                                                            break;
                                     default:
                                         $.alert(JSON.parse(data).err_msg);
                               }
                          }else{
                  	  		data=JSON.parse(data);
                            data=JSON.parse(my.cryptoASEdencrypt(data.data,key));
                            console.log(data)
                            var flight_list=data.flight_list
                            var count=flight_list.length
                            for(var i=0;i<count;i++){
                            	var list='<div class="flightInfoList clearfix" data-ddstores_id="'+flight_list[i].ddstores_id+'" data-qfstores_id="'+flight_list[i].qfstores_id+'"><div class="flightInfoList-a"><div class="info-t">'
                            	     +flight_list[i].DepTime+'</div><div class="info-b">'
                		             +flight_list[i].DepCity+flight_list[i].DepTerminal+'</div></div><div class="flightInfoList-b"><div class="info-t"></div><div class="info-b"></div></div><div class="flightInfoList-c"><div class="info-t">'
                		             +flight_list[i].ArrTime+'</div><div class="info-b">'
                		             +flight_list[i].ArrCity+flight_list[i].ArrTerminal+'</div></div><div class="flightInfoList-d"><div class="info-t">'
                		             +flight_list[i].Airline+'</div><div class="info-b">'
                		             +flight_list[i].FlightNum+'</div></div>'
                		             $('.flightInfo-box-cx-list').append(list)
                            }

                            $('.info-head-c').on('touchend',function(){
                            	 flight_list=flight_list.reverse()
                            	$('.flightInfo-box-cx-list').empty()
                                for(var i=0;i<count;i++){
                            	  var list='<div class="flightInfoList clearfix" data-ddstores_id="'+flight_list[i].ddstores_id+'" data-qfstores_id="'+flight_list[i].qfstores_id+'"><div class="flightInfoList-a"><div class="info-t">'
                            	     +flight_list[i].DepTime+'</div><div class="info-b">'
                		             +flight_list[i].DepCity+flight_list[i].DepTerminal+'</div></div><div class="flightInfoList-b"><div class="info-t"></div><div class="info-b"></div></div><div class="flightInfoList-c"><div class="info-t">'
                		             +flight_list[i].ArrTime+'</div><div class="info-b">'
                		             +flight_list[i].ArrCity+flight_list[i].ArrTerminal+'</div></div><div class="flightInfoList-d"><div class="info-t">'
                		             +flight_list[i].Airline+'</div><div class="info-b">'
                		             +flight_list[i].FlightNum+'</div></div>'
                		             $('.flightInfo-box-cx-list').append(list)
                               }

                            })
                           }
                 	  	}
                 	  });
                 });
                 var change_to_form_date=function(startTime){
                 	var newdata_1 = DateTimeUtils.Convert.parseDate(startTime);
                 	console.log(newdata_1)
                 	console.log(newdata_1.getFullYear()+"-"+formatNumber(newdata_1.getMonth()+1)+"-"+formatNumber(newdata_1.getDate())+" "+formatNumber(newdata_1.getHours()-1)+":"+minutesss(newdata_1.getMinutes())+":00")
                    $(".start_parkIndex_start").datetimePickerCustom('setCustomValue',newdata_1.getFullYear()+"-"+formatNumber(newdata_1.getMonth()+1)+"-"+formatNumber(newdata_1.getDate())+" "+formatNumber(newdata_1.getHours()-1)+":"+minutesss(newdata_1.getMinutes())+":00");
                    $(".end_parkIndex_end").datetimePickerCustom('setCustomValue',newdata_1.getFullYear()+"-"+formatNumber(newdata_1.getMonth()+1)+"-"+formatNumber(newdata_1.getDate()+1)+" "+formatNumber(newdata_1.getHours()-1)+":"+minutesss(newdata_1.getMinutes())+":00");
                 }

                 $('.flightInfo-box-cx-list').off('click').on('click','.flightInfoList',function(){
                 	   var flightNum=$(this).find('.flightInfoList-d .info-b').text()
                 	   var startTime=$('.flightInfo-box-cx-time').val()+' '+$(this).find('.flightInfoList-a .info-t').text()+':00'
                 	   var endTime=$('.flightInfo-box-cx-time').val()+' '+$(this).find('.flightInfoList-c .info-t').text()+':00'
                 	   $('.flightInfo-parkIndex').val(flightNum+'-'+$('.flightInfo-box-cx-time').val())
                 	   $('[name=store_id]').val($(this).attr('data-qfstores_id'));
                 	   change_to_form_date(startTime);
                       sessionStorage.flight_num=$(this).find('.flightInfoList-d .info-b').text()//航班号码
                       sessionStorage.flight_store_id=$(this).attr('data-qfstores_id') //起飞门店id

                       // page_parkRecommend.js 接收参数:
                       sessionStorage.flight_endPlace_id=$(this).attr('data-ddstores_id');//到达门店id
                       sessionStorage.flight_endPlace=$(this).find('.flightInfoList-c .info-b').text();//到达机场
                       // page_parkRecommend.js 接收参数:


                 	   $('.flight-select span').eq(0).text($('.flightInfo-box-cx-time').val())
                 	   $('.flight-select span').eq(1).text($(this).find('.flightInfoList-a .info-t').text()+'-'+$(this).find('.flightInfoList-c .info-t').text())
                       $('.flight-select span').eq(2).text($(this).find('.flightInfoList-d .info-b').text())
                       $('.flight-select').show()

                       $('.flight-select-index span').eq(0).text($('.flightInfo-box-cx-time').val())
                 	   $('.flight-select-index span').eq(1).text($(this).find('.flightInfoList-a .info-t').text()+'-'+$(this).find('.flightInfoList-c .info-t').text())
                       $('.flight-select-index span').eq(2).text($(this).find('.flightInfoList-d .info-t').text())
                       $('.flight-select-index').show()
                       $('.dt-content').css('top','11.4rem')
                       $.router.load('#page_parkIndex')
                 })
                 $('.flight-select').click(function(){
                       $.router.load('#page_parkIndex')
                  })
                 //自定义航班查询
                 $('.flightDate').off().calendar({
                    // value: ['2015-12-05']
                    onClose:function(){
                    	if($(".flightNum").val().length>3){
                                        //航班接口start
                                        $.showPreloader()
                                        var airinfo={flight_name:$('.flightNum').val().toUpperCase(),flight_date:$('.flightDate').val(),}
                                        console.log(airinfo)
                                        airinfo=my.cryptoASEencrypt(JSON.stringify(airinfo),key)
                                        $.ajax({
                                            url:api.searchflightinfo,
                                            type: 'post',
                                            async:true,
                                            timeout:30000,
                                            data:{'api_value':airinfo},
                                            error:function(XMLHttpRequest,status){
                                            	console.log(status)
                                            	$.hidePreloader()
                                            	if(status=='timeout'){
                                            		$.toast('请求超时..')
                                                }else if(status=='error'){
                                            		$.toast('服务器请求失败..')
                                                }else{
                                                	$.toast("航班信息获取失败");
                                                }


                                            },
                                            success:function(data){
                                            	$.hidePreloader()
                                                if(data.match("err_code")!=null){
                                                    switch(JSON.parse(data).err_code)
                                                    {
                                                        case 40003:
                                                            my.tokenExpire()
                                                            break;
                                                        default:
                                                            $.toast(JSON.parse(data).err_msg);
                                                    }
                                                }else{
                                                	data=JSON.parse(data)
                                                    data=JSON.parse(my.cryptoASEdencrypt(data.data,key))
                                                    console.log(data)
                                                    $(".qftime_full").text(data.from_store.qftime_full);
                                                    $(".ddtime_full").text(data.to_store.ddtime_full);
                                                	$('.flightInfo-ok').addClass('active_btn').off('click').on('click',function(){

                                                		$('.flight-select span').eq(0).text($('.flightInfo-box-cx-time').val())
                 	                                    $('.flight-select span').eq(1).text(data.from_store.qftime+'-'+data.to_store.ddtime)
                                                        $('.flight-select span').eq(2).text(data.from_store.fno)
                                                        $('.flight-select').show()
                                                        $('.dt-content').css('top','6.4rem')

                                                        $('.flight-select-index span').eq(0).text($('.flightDate').val())
                 	                                    $('.flight-select-index span').eq(1).text(data.from_store.qftime+'-'+data.to_store.ddtime)
                                                        $('.flight-select-index span').eq(2).text(data.from_store.company)
                                                        $('.flight-select-index').show()
                                                        $('.dt-content').css('top','6.4rem')

                      	                                $('.flightInfo-parkIndex').val($('.flightNum').val()+'-'+$('.flightDate').val())
                      	                                $('[name=flight_number]').val($('.flightNum').val())
                                                        $('[name=flight_time]').val($('.flightDate').val());



//                                                      $(".chufaflight").text($(".chufa").val());
//                                                      $(".chufatime").text(data.from_store.qftime_full);
//                                                      $(".daodaflight").text($(".daoda").val());
//                                                      $(".daodatime").text(data.to_store.ddtime_full);
//                                                      $(".flightinfodetails").show();

                                                         sessionStorage.parkPlace_parkReco=JSON.stringify(data)
                                                         $('[name=store_id]').val(data.from_store.stores_id)
                                                         sessionStorage.flight_num=$('.flightNum').val()
                                                         sessionStorage.flight_store_id=data.from_store.stores_id


                                                         // page_parkRecommend.js 接收参数:
                                                         sessionStorage.flight_endPlace=data.to_store.city_name
                                                         sessionStorage.flight_endPlace_id=data.to_store.stores_id

                                                         sessionStorage.flight_startPlace=data.from_store.city_name
                                                         sessionStorage.flight_startPlace_id=data.from_store.city_id

                                                         change_to_form_date(data.from_store.qftime_full);
                                                          $.router.load('#page_parkIndex')
                                                    }).css('color','#fff')



                                                    $('.flightInfo-airport_select select').empty();
                                                    //airport_name
                                                    $('.chufa').append('<option value='+data.from_store.stores_id+'>'+data.from_store.airport_name+'</option>')
                                                    $('.daoda').append('<option value='+data.to_store.stores_id+'>'+data.to_store.airport_name+'</option>')

                                                    //$('.chufa').append('<option value='+data.from_store.stores_id+'>'+data.from_store.city_name+data.from_store.from_simple+'</option>')
                                                    //$('.daoda').append('<option value='+data.to_store.stores_id+'>'+data.to_store.city_name+data.to_store.to_simple+'</option>')
                                                    if(data.trans_list.length>0){
                                                    	var op=[];
                                                    	for(var i=0,len=data.trans_list.length;i<len;i++){
                                                    		op[i]='<option value='+data.to_store.stores_id+'>'+data.to_store.to_simple+'</option>'
                                                    	}
                                                    	$('.chufa,.daoda').append(op.join(' '))

                                                    }
                                                    $('.flightInfo-box-zdy-list-box').show()
                                 /*      $('.flightInfo-box-zdy-list').empty()
                                                  var list='<div class="flightInfoList clearfix"><div class="flightInfoList-a"><div class="info-t">'
                          	     +data.from_store.qftime+'</div><div class="info-b">'
              		             +data.from_store.city_name+data.from_store.fromTerminal+'</div></div><div class="flightInfoList-b"><div class="info-t"></div><div class="info-b"></div></div><div class="flightInfoList-c"><div class="info-t">'
              		             +data.to_store.ddtime+'</div><div class="info-b">'
              		             +data.to_store.city_name+data.to_store.toTerminal+'</div></div><div class="flightInfoList-d"><div class="info-t">'
             		             +data.from_store.company+'</div><div class="info-b">'
              		             +data.from_store.fno+'</div></div>'
              		             $('.flightInfo-box-zdy-list').append(list)

                                                 */



                                                }
                                            }
                                        });
                                    }else{
                                        $(".flightNum").val('');
                                        $.toast("亲~请先输入正确的航班号");
                                    }
                          }
                       });
                     //自定义航班选择机场
                   $(".chufa,.daoda").on("change",function(){
                   	   $('.flightInfo-ok').addClass('active_btn').off('click').on('click',function(){
                   	   	    $('[name=store_id]').val($('.chufa').val())  //设置出发门店id
                   	   	    sessionStorage.flight_store_id=$('.chufa').val()
                   	   	    sessionStorage.flight_endPlace_id=$('.daoda').val()//设置到达门店id
                   	   	    $.router.load('#page_parkIndex')
                   	   })
                   })
                     //航班接口end
})